package com.jiangyg.mall.coupon.service;

import com.jiangyg.mall.coupon.dao.MemberPriceDao;
import com.jiangyg.mall.coupon.dao.SkuFullReductionDao;
import com.jiangyg.mall.coupon.dao.SkuLadderDao;
import com.jiangyg.mall.coupon.dto.MemberPriceDTO;
import com.jiangyg.mall.coupon.dto.SkuConcessionDTO;
import com.jiangyg.mall.coupon.entity.MemberPrice;
import com.jiangyg.mall.coupon.entity.SkuFullReduction;
import com.jiangyg.mall.coupon.entity.SkuLadder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SkuConcessionServiceImpl implements SkuConcessionService {

    private final SkuLadderDao ladderDao;

    private final MemberPriceDao memberPriceDao;

    private final SkuFullReductionDao fullReductionDao;

    @Autowired
    public SkuConcessionServiceImpl(SkuLadderDao ladderDao,
                                    MemberPriceDao memberPriceDao,
                                    SkuFullReductionDao fullReductionDao) {
        this.ladderDao = ladderDao;
        this.memberPriceDao = memberPriceDao;
        this.fullReductionDao = fullReductionDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SkuConcessionDTO dto) {
        // 1. 商品阶梯价格（满几件打几折）
        if (dto.getFullcount() != null && dto.getFullcount() > 0) {
            final SkuLadder ladder = new SkuLadder();
            ladder.setSkuId(dto.getSkuId());
            ladder.setFullcount(dto.getFullcount());
            ladder.setDiscount(dto.getDiscount());
            ladder.setSuperposable(dto.getCountSuperposable());
            this.ladderDao.insert(ladder);
        }
        // 2. 商品满减信息（满多少减多少）
        if (dto.getFullPrice() != null && dto.getFullPrice().compareTo(BigDecimal.valueOf(0)) >= 1) {
            final SkuFullReduction fullReduction = new SkuFullReduction();
            fullReduction.setSkuId(dto.getSkuId());
            fullReduction.setFullPrice(dto.getFullPrice());
            fullReduction.setReducePrice(dto.getReducePrice());
            fullReduction.setSuperposable(dto.getPriceSuperposable());
            this.fullReductionDao.insert(fullReduction);
        }
        // 3. 会员价
        final List<MemberPriceDTO> memberPrices = dto.getMemberPrice();
        if (CollectionUtils.isNotEmpty(memberPrices)) {
            memberPrices.forEach(mp -> {
                final MemberPrice memberPrice = new MemberPrice();
                memberPrice.setSkuId(dto.getSkuId());
                memberPrice.setLevelId(mp.getId());
                memberPrice.setLevelName(mp.getName());
                memberPrice.setPrice(mp.getPrice());
                memberPrice.setSuperposable(mp.getSuperposable());
                this.memberPriceDao.insert(memberPrice);
            });
        }
        return true;
    }

}
